package com.lw.extraceim.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.lw.extraceim.R;
import com.lw.extraceim.app.AppConstants;
import com.lw.extraceim.loader.ExpressSheetLoader;
import com.lw.extraceim.model.CustomerInfo;
import com.lw.extraceim.model.ExpressSheet;
import com.lw.extraceim.net.HttpResponseParam;
import com.lw.extraceim.net.ResponseCode;
import com.lw.extraceim.net.callback.HttpResponseParamCallback;
import com.lw.extraceim.ui.activity.base.BaseActivity;
import com.lw.extraceim.ui.activity.es.ExpressSheetInfoActivity;
import com.lw.extraceim.ui.adapter.ExpressSheetAdapter;
import com.lw.extraceim.util.DialogUtil;
import com.lw.extraceim.util.JsonUtils;
import com.lw.extraceim.util.ZxingUtil;
import com.lw.extraceim.zxing.ExtraceCaptureActivity;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * @Author : liwei
 * @Email : 1759840027@qq.com
 * @Description : 搜索界面
 * @Date : 2019/4/16 10:23
 */
public class SearchActivity extends BaseActivity {
    private static final String TAG = "SearchActivity";

    private ListView mSearchListView;
    private String key;//搜索的内容

    private SearchView mSearchView;
    private ImageButton mScanButton;
    private Toolbar mToolbar;

    private ExpressSheetLoader esLoader;
    private List<ExpressSheet> esList;
    private ExpressSheetAdapter esAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_search);
        mToolbar = findViewById(R.id.toolbar);
        mSearchView = findViewById(R.id.search_view);
        mScanButton = findViewById(R.id.scan_button);
        initToolBar();
        mSearchListView = findViewById(R.id.search_list_view);

    }

    /**
     * 初始化toolBar
     */
    protected void initToolBar() {
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                queryEsList(query);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });

        mScanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startQRScan(SearchActivity.this, ExtraceCaptureActivity.class, "扫描条形码", ExtraceCaptureActivity.SCAN_SEARCH);
            }
        });

    }

    /**
     * 查询快件信息
     *
     * @param esId
     */
    public void queryEsList(String esId) {
        ExpressSheet expressSheet = new ExpressSheet();
        expressSheet.setId(esId);
        esLoader.getEsList(expressSheet, new HttpResponseParamCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                alert("网络错误" + e.getMessage());
            }

            @Override
            public void onResponse(HttpResponseParam response, int id) {
                Log.d(TAG, "onResponse: " + response.reseponseJson);
                if (response.code.getCode() == ResponseCode.SUCESS) {
                    try {
                        esList = JsonUtils.fromJson(response.reseponseJson.getJSONArray("esList").toString(), new TypeToken<List<ExpressSheet>>() {
                        });
                        for (ExpressSheet expressSheet : esList) {
                            expressSheet.onLoad();
                        }
                        refreshUI();
                    } catch (JSONException e) {
                        e.printStackTrace();
                        DialogUtil.alert(SearchActivity.this, "解析json失败" + e.getMessage());
                    }
                } else {
                    DialogUtil.alert(SearchActivity.this, response.code.getMessage());
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ExtraceCaptureActivity.SCAN_SEARCH: {
                String esId = ZxingUtil.scanResult(resultCode, data);//快件编号
                queryEsList(esId);
                break;
            }
        }
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        key = intent.getStringExtra("key");
        esLoader = new ExpressSheetLoader(this);
    }

    @Override
    public void refreshUI() {
        if (esAdapter == null) {
            esAdapter = new ExpressSheetAdapter(esList, this);
            mSearchListView.setAdapter(esAdapter);
        } else {
            esAdapter.setData(esList);
        }
    }

    @Override
    public void initEvent() {
        mSearchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ExpressSheetInfoActivity.actionStart(SearchActivity.this, esList.get(position));

            }
        });
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }


    public static void actionStart(Context context, String action, String key) {
        Intent intent = new Intent(context, SearchActivity.class);
        intent.putExtra("action", action);
        intent.putExtra("key", key);
        context.startActivity(intent);
    }
}
